﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace PacMan
{
    
    /// <summary>
    /// Provides logic for character named Inky.
    /// </summary>
    class Inky : Character
    {
        public Inky(int x, int y, int speed)
            : base(x, y, speed)
        {
            this.Width = 40;
            this.Height = 40;
        }

        public void GetOut()
        {
            if (Position == Position.Left)
            {
                Position = Position.Right;
                X += Speed;
                return;
            }
            if (Position == Position.Right)
            {
                Position = Position.Left;
                X -= Speed;
                return;
            }
            if (Position == Position.Up)
            {
                Position = Position.Down;
                Y += Speed;
                return;
            }
            if (Position == Position.Down)
            {
                Position = Position.Up;
                Y -= Speed;
                return;
            }
        }

        public bool CantMove(Inky pinky, RectangleF[] maze)
        {
            for (int i = 0; i < maze.Length; i++)
            {
                if (pinky.Obstacles(maze[i]) == true)
                {
                    return true;
                }
            }
            return false;
        }
        
        public void Move(RectangleF[] maze)
        {
            Random rand = new Random();
            bool obstacle = false;
            int choosePosition = rand.Next(1, 5);
            int currentPosition = Convert.ToInt32(this.Position);

            obstacle = CantMove(this, maze);

            if (obstacle == true)
            {
                while (choosePosition == currentPosition)
                {
                    choosePosition = rand.Next(1, 5);
                }
                GetOut();

                bool cantMove;

                if (choosePosition == 1)
                {
                    Inky temp = new Inky(this.X, this.Y, this.Speed);
                    temp.X -= Speed;
                    cantMove = CantMove(temp, maze);
                    if (cantMove == true)
                    {
                        while (choosePosition == currentPosition)
                        {
                            choosePosition = rand.Next(2, 5);
                        }
                    }
                }

                if (choosePosition == 2)
                {
                    Inky temp = new Inky(this.X, this.Y, this.Speed);
                    temp.X += Speed;
                    cantMove = CantMove(temp, maze);
                    if (cantMove == true)
                    {
                        while (choosePosition == 2 || choosePosition == currentPosition)
                        {
                            choosePosition = rand.Next(1, 5);
                        }
                    }
                }
                if (choosePosition == 3)
                {
                    Inky temp = new Inky(this.X, this.Y, this.Speed);
                    temp.Y -= Speed;
                    cantMove = CantMove(temp, maze);
                    if (cantMove == true)
                    {
                        while (choosePosition == 3 || choosePosition == currentPosition)
                        {
                            choosePosition = rand.Next(1, 5);
                        }
                    }
                }
                if (choosePosition == 4)
                {
                    Inky temp = new Inky(this.X, this.Y, this.Speed);
                    temp.Y += Speed;
                    cantMove = CantMove(temp, maze);
                    if (cantMove == true)
                    {
                        while (choosePosition == 4 || choosePosition == currentPosition)
                        {
                            choosePosition = rand.Next(1, 5);
                        }
                    }
                }
            }

            if (obstacle == true)
            {
                switch (choosePosition)
                {
                    case 1:
                        {
                            Position = Position.Left;
                            break;
                        }
                    case 2:
                        {
                            Position = Position.Right;
                            break;
                        }
                    case 3:
                        {
                            Position = Position.Up;
                            break;
                        }
                    case 4:
                        {
                            Position = Position.Down;
                            break;
                        }
                }
            }
            else
            {
                if (Position == Position.Down)
                {
                    Y += Speed;
                }
                if (Position == Position.Right)
                {
                    X += Speed;
                }
                if (Position == Position.Up)
                {
                    Y -= Speed;
                }
                if (Position == Position.Left)
                {
                    X -= Speed;
                }
            }

        }
    }
}
